package com.ruoyi.main.controller;

import java.util.List;

import com.ruoyi.main.service.IMainPostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.main.domain.MainRole;
import com.ruoyi.main.service.IMainRoleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 角色管理Controller
 * 
 * @author chao
 * @date 2024-07-23
 */
@Controller
@RequestMapping("/main/role")
public class MainRoleController extends BaseController
{
    private String prefix = "main/role";

    @Autowired
    private IMainRoleService mainRoleService;

    @Autowired
    private IMainPostService mainPostService;

    @RequiresPermissions("main:role:view")
    @GetMapping()
    public String role(Model model)
    {
        model.addAttribute("PostNames",mainPostService.getAllNormalNames());
        return prefix + "/role";
    }

    /**
     * 查询角色管理列表
     */
    @RequiresPermissions("main:role:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MainRole mainRole)
    {
        startPage();
        List<MainRole> list = mainRoleService.selectMainRoleList(mainRole);
        return getDataTable(list);
    }

    /**
     * 导出角色管理列表
     */
    @RequiresPermissions("main:role:export")
    @Log(title = "角色管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MainRole mainRole)
    {
        List<MainRole> list = mainRoleService.selectMainRoleList(mainRole);
        ExcelUtil<MainRole> util = new ExcelUtil<MainRole>(MainRole.class);
        return util.exportExcel(list, "角色管理数据");
    }

    /**
     * 新增角色管理
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        model.addAttribute("PostNames",mainPostService.getAllNormalNames());
        return prefix + "/add";
    }

    /**
     * 新增保存角色管理
     */
    @RequiresPermissions("main:role:add")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MainRole mainRole)
    {
        return toAjax(mainRoleService.insertMainRole(mainRole));
    }

    /**
     * 修改角色管理
     */
    @RequiresPermissions("main:role:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        MainRole mainRole = mainRoleService.selectMainRoleById(id);
        mmap.put("mainRole", mainRole);
        mmap.put("PostNames",mainPostService.getAllNormalNames());
        return prefix + "/edit";
    }

    /**
     * 修改保存角色管理
     */
    @RequiresPermissions("main:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MainRole mainRole, Model model)
    {
        model.addAttribute("PostNames",mainPostService.getAllNormalNames());
        return toAjax(mainRoleService.updateMainRole(mainRole));
    }

    /**
     * 删除角色管理
     */
    @RequiresPermissions("main:role:remove")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mainRoleService.deleteMainRoleByIds(ids));
    }
}
